<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CORS Test Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
        }
        .success {
            border-left: 4px solid #28a745;
        }
        .error {
            border-left: 4px solid #dc3545;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🌐 Gateway CORS Configuration Test</h1>
        <p>This page tests CORS configuration on your Spring Cloud Gateway.</p>
        
        <div class="test-section">
            <h3>Test 1: Simple GET Request</h3>
            <button onclick="testSimpleGet()">Test GET /test/ping</button>
            <div id="result1" class="result"></div>
        </div>

        <div class="test-section">
            <h3>Test 2: POST Request with Authorization</h3>
            <button onclick="testAuthPost()">Test POST with Token</button>
            <div id="result2" class="result"></div>
        </div>

        <div class="test-section">
            <h3>Test 3: Preflight OPTIONS Request</h3>
            <button onclick="testPreflight()">Test Preflight Request</button>
            <div id="result3" class="result"></div>
        </div>

        <div class="test-section">
            <h3>Test 4: Provider Service via Gateway</h3>
            <button onclick="testProvider()">Test Provider Service</button>
            <div id="result4" class="result"></div>
        </div>
    </div>

    <script>
        const GATEWAY_BASE_URL = 'http://localhost:9000';

        function logResult(elementId, message, isError = false) {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.className = `result ${isError ? 'error' : 'success'}`;
        }

        async function testSimpleGet() {
            try {
                const response = await fetch(`${GATEWAY_BASE_URL}/test/ping`, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                const corsHeaders = {
                    'Access-Control-Allow-Origin': response.headers.get('Access-Control-Allow-Origin'),
                    'Access-Control-Allow-Credentials': response.headers.get('Access-Control-Allow-Credentials')
                };
                
                logResult('result1', `✅ Success!\nStatus: ${response.status}\nData: ${JSON.stringify(data, null, 2)}\nCORS Headers: ${JSON.stringify(corsHeaders, null, 2)}`);
            } catch (error) {
                logResult('result1', `❌ Error: ${error.message}`, true);
            }
        }

        async function testAuthPost() {
            try {
                const response = await fetch(`${GATEWAY_BASE_URL}/test/auth-required`, {
                    method: 'GET',
                    headers: {
                        'Authorization': 'Bearer mytoken',
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                const corsHeaders = {
                    'Access-Control-Allow-Origin': response.headers.get('Access-Control-Allow-Origin'),
                    'Access-Control-Allow-Credentials': response.headers.get('Access-Control-Allow-Credentials'),
                    'X-User-Id': response.headers.get('X-User-Id')
                };
                
                logResult('result2', `✅ Success!\nStatus: ${response.status}\nData: ${JSON.stringify(data, null, 2)}\nHeaders: ${JSON.stringify(corsHeaders, null, 2)}`);
            } catch (error) {
                logResult('result2', `❌ Error: ${error.message}`, true);
            }
        }

        async function testPreflight() {
            try {
                const response = await fetch(`${GATEWAY_BASE_URL}/test/ping`, {
                    method: 'OPTIONS',
                    headers: {
                        'Access-Control-Request-Method': 'POST',
                        'Access-Control-Request-Headers': 'Authorization,Content-Type'
                    }
                });
                
                const corsHeaders = {
                    'Access-Control-Allow-Origin': response.headers.get('Access-Control-Allow-Origin'),
                    'Access-Control-Allow-Methods': response.headers.get('Access-Control-Allow-Methods'),
                    'Access-Control-Allow-Headers': response.headers.get('Access-Control-Allow-Headers'),
                    'Access-Control-Max-Age': response.headers.get('Access-Control-Max-Age')
                };
                
                logResult('result3', `✅ Preflight Success!\nStatus: ${response.status}\nCORS Headers: ${JSON.stringify(corsHeaders, null, 2)}`);
            } catch (error) {
                logResult('result3', `❌ Error: ${error.message}`, true);
            }
        }

        async function testProvider() {
            try {
                const response = await fetch(`${GATEWAY_BASE_URL}/provider/user/getUserById/1`, {
                    method: 'GET',
                    headers: {
                        'Authorization': 'Bearer mytoken',
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                const corsHeaders = {
                    'Access-Control-Allow-Origin': response.headers.get('Access-Control-Allow-Origin'),
                    'Access-Control-Allow-Credentials': response.headers.get('Access-Control-Allow-Credentials')
                };
                
                logResult('result4', `✅ Provider Service Success!\nStatus: ${response.status}\nData: ${JSON.stringify(data, null, 2)}\nCORS Headers: ${JSON.stringify(corsHeaders, null, 2)}`);
            } catch (error) {
                logResult('result4', `❌ Error: ${error.message}`, true);
            }
        }

        // 页面加载时显示当前域名
        document.addEventListener('DOMContentLoaded', function() {
            console.log('Current Origin:', window.location.origin);
            console.log('Gateway URL:', GATEWAY_BASE_URL);
        });
    </script>
</body>
</html> 